// pages/my/orderHistry/orderHistry.js
var util = require('../../utils/util.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    show: "",
    myBoxList: {}
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    wx.setNavigationBarTitle({
      title: '我的箱子'
    })

    //获取我的箱子列表
    var that = this;
    var _uid = wx.getStorageSync("userInfo").userInfo.uid;
    const requestGetMyBox = wx.request({
      url: "https://boxapi.supergk.com/server/courier/courier_box_list",
      data: {
        // courier_id : _uid,
        courier_id: 7,
        status: "1"
      },
      success: function (res) {
        console.log(res);
        if (res.data.code == 0) {
          that.setData({
            myBoxList: res.data.data,
          })
        } else {
          console.log(res.data.msg);
        }
      }
    })
  },
  clickScan: function () {
    var that = this;
    var show;
    wx.scanCode({
      success: (res) => {
        this.show = "--result:" + res.result + "--scanType:" + res.scanType + "--charSet:" + res.charSet + "--path:" + res.path;
        that.setData({
          show: this.show,
          scanResult: res.result
        })
        wx.showToast({
          title: '扫码成功',
          icon: 'success',
          duration: 2000
        })

        // 扫码获取 箱子编号后，请求接口，获取接口参数
        var _uid = wx.getStorageSync("userInfo").userInfo.uid;
        var _box_bn = res.result;
        const requestGetRequestUrl = wx.request({
          url: "https://boxapi.supergk.com/server/courier/courier_sm",
          header: {
            "content-type": "application/x-www-form-urlencoded"
          },
          method: "POST",
          data: util.json2Form({
            uid: _uid,
            box_bn: _box_bn
          }),
          success: function (e) {
            console.log(e)
            if (e.data.code == 0) {
              var _call_back_code = e.data.data.call_back_code;
              if (_call_back_code == 1) {
                that.courierGetBox(_box_bn);
              } else if (_call_back_code == 2) {
                that.courierUpdateOrder(_box_bn);
              } else if (_call_back_code == 3) {
                that.courierEndOrder(_box_bn);
              } else if (_call_back_code == 4) {
                that.courierOpenBoxNow(_box_bn);
              }
            } else {
              console.log(e.data.msg)
            }
          }
        })

      },
      fail: (res) => {
        wx.showToast({
          title: '失败',
          icon: 'success',
          duration: 2000
        })
      },
      complete: (res) => {
      }
    })
  },

  //1、 快递员获取箱子
  courierGetBox: function (_box_bn) {
    var _uid = wx.getStorageSync("userInfo").userInfo.uid;
    const requestGetRequestUrl = wx.request({
      url: "https://boxapi.supergk.com/server/courier/get_box",
      header: {
        "content-type": "application/x-www-form-urlencoded"
      },
      method: "POST",
      data: util.json2Form({
        uid: _uid,
        box_bn: _box_bn
      }),
      success: function (result) {
        console.log(result)
        if (result.data.code == 0) {
          console.log(result.data.msg)
          wx.showToast({
            title: result.data.msg,
            icon: "success"
          })
        } else {
          console.log(result.data.msg)
          wx.showModal({
            title: '提示',
            content: result.data.msg,
            showCancel: false
          })
        }
      }
    })
  },

  //2、 快递员扫码箱子同步订单
  courierUpdateOrder: function (_box_bn) {
    var _uid = wx.getStorageSync("userInfo").userInfo.uid;
    const requestGetRequestUrl = wx.request({
      url: "https://boxapi.supergk.com/server/courier/update_order_courier",
      header: {
        "content-type": "application/x-www-form-urlencoded"
      },
      method: "POST",
      data: util.json2Form({
        courier_id: _uid,
        box_bn: _box_bn
      }),
      success: function (result) {
        console.log(result)
        if (result.data.code == 0) {
          console.log(result.data.msg)
          wx.showToast({
            title: result.data.msg,
            icon: "success"
          })
        } else {
          console.log(result.data.msg)
          wx.showModal({
            title: '提示',
            content: result.data.msg,
            showCancel: false
          })
        }
      }
    })
  },

  //3、 快递员结束订单
  courierEndOrder: function (_box_bn) {
    var _uid = wx.getStorageSync("userInfo").userInfo.uid;
    const requestGetRequestUrl = wx.request({
      url: "https://boxapi.supergk.com/server/courier/end_order",
      header: {
        "content-type": "application/x-www-form-urlencoded"
      },
      method: "POST",
      data: util.json2Form({
        uid: _uid,
        box_bn: _box_bn
      }),
      success: function (result) {
        console.log(result)
        if (result.data.code == 0) {
          console.log(result.data.msg)
          wx.showToast({
            title: result.data.msg,
            icon: "success"
          })
        } else {
          console.log(result.data.msg)
          wx.showModal({
            title: '提示',
            content: result.data.msg,
            showCancel: false
          })
        }
      }
    })
  },

  //4、 快递员结束订单
  courierOpenBoxNow: function (_box_bn) {
    var _uid = wx.getStorageSync("userInfo").userInfo.uid;
    const requestGetRequestUrl = wx.request({
      url: "https://boxapi.supergk.com/server/box/courier_open_box_now",
      header: {
        "content-type": "application/x-www-form-urlencoded"
      },
      method: "POST",
      data: util.json2Form({
        uid: _uid,
        box_bn: _box_bn
      }),
      success: function (result) {
        console.log(result)
        if (result.data.code == 0) {
          console.log(result.data.msg)
          wx.showToast({
            title: result.data.msg,
            icon:"success"
          })
        } else {
          console.log(result.data.msg)
          wx.showModal({
            title: '提示',
            content: result.data.msg,
            showCancel: false
          })
        }
      }
    })
  },


  moveToInformant: function (e) {
    const dataset = e.currentTarget.dataset;
    wx.navigateTo({
      url: '../informant/informant?id=' + dataset.id + '&num=' + dataset.num
    })
    console.log(dataset.id);
  },

  // 点击还箱子
  moveToBackBox: function (e) {
    var that = this;
    var _uid = wx.getStorageSync("userInfo").userInfo.uid;
    const dataset = e.currentTarget.dataset;
    var _box_id = dataset.num;

    const requestBackBox = wx.request({
      url: "https://boxapi.supergk.com/server/courier/back_box",
      header: {
        "content-type": "application/x-www-form-urlencoded"
      },
      data: util.json2Form({
        uid: _uid,
        box_bn: _box_id
      }),
      method: "POST",
      success: function (res) {
        console.log(res);
        if (res.data.code == "0") {
          setTimeout((function callback() {
            wx.navigateTo({
              url: '../success/success',
            })
          }).bind(this), 1000);
        } else {
          console.log(res.data.msg)
        }
      }
    })
  },
  // 点击开箱
  moveToOpen: function (e) {
    const dataset = e.currentTarget.dataset;
    var _box_bn = dataset.num;
    var _uid = wx.getStorageSync("userInfo").userInfo.uid;
    const requestOpenBox = wx.request({
      url: "https://boxapi.supergk.com/server/box/courier_open_box_now",
      header: {
        "content-type": "application/x-www-form-urlencoded"
      },
      method: "POST",
      data: util.json2Form({
        box_bn: _box_bn,
        uid: _uid
      }),
      success: function (res) {
        console.log(res);
        if (res.data.code == 0) {
          wx.showToast({
            title: '开箱成功',
            icon: "success"
          })
        } else {
          console.log(res.data.msg)
          wx.showToast({
            title: res.data.msg,
            icon: "loading"
          })
        }
      }
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})